package com.xxx.video.login.presenters;

import android.content.Context;
import android.provider.Settings;
import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.xxx.video.R;
import com.xxx.video.commen.presenters.BasePresenter;
import com.xxx.video.commen.utils.ResponseUtils;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.commen.utils.ToastUtils;
import com.xxx.video.login.beans.LoginSuccessBean;
import com.xxx.video.network.listeners.CommonResponseListener;
import com.xxx.video.network.utils.NetWorkUtils;

public class LoginPresenter extends BasePresenter<ILoginView> {

    public LoginPresenter(@NonNull Context context, ILoginView baseView) {
        super(context, baseView);
    }

    /**
     * login
     */
    public void login(String longitude, String latitude) {
        String userName = getBaseView() == null ? "" : getBaseView().getUserName();
        String pwd = getBaseView() == null ? "" : getBaseView().getPwd();
        if (TextUtils.isEmpty(userName)) {
            ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_login_username_can_not_empty));
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            ToastUtils.showError(StringUtils.getInstance().getString(R.string.tip_login_pwd_can_not_empty));
            return;
        }
        String androidId = Settings.System.getString(context.getContentResolver(), Settings.System.ANDROID_ID);
        showProgressDialog("loading");

        NetWorkUtils.getInstance().login(userName, pwd, androidId, longitude, latitude, new CommonResponseListener<LoginSuccessBean>() {
            @Override
            public void onSuccess(LoginSuccessBean successOb, String result) {
                hideProgressDialog();
                if (ResponseUtils.getInstance().isNoError(successOb)) {
                    if (getBaseView() != null) {
                        getBaseView().loginSuccess(successOb);
                    }
                }
            }

            @Override
            public void onFailure(Object failureObj) {
                hideProgressDialog();
            }
        });
    }

    /**
     * start forget pwd activity
     */
    public void startForgetPwdActivity() {
        if (getBaseView() != null) {
            getBaseView().startInputUserNameActivity();
        }
    }
}
